package at.ltasat.util;

public final class Average {

	private final double average, deviation;
	
	public static Average get(Iterable<Integer> values) {
		int count = 0;
		double average = 0;
		for(Integer value : values){
			average += value;
			count++;
		}
		average /= count;
		
		double deviation = 0;
		for(Integer value : values){
			double delta = value - average;
			deviation += (delta * delta);
		}
		deviation /= count;
		deviation = Math.sqrt(deviation);
		
		return new Average(average, deviation);
	}
	
	private Average(double average, double deviation) {
		this.average = average;
		this.deviation = deviation;
	}

	public double average() {
		return average;
	}

	public double deviation() {
		return deviation;
	}

	@Override
	public int hashCode() {
		return (int) (Double.doubleToLongBits(average) + Double.doubleToLongBits(deviation));
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof Average) {
			Average other = (Average) obj;
			return other.average == average && other.deviation == deviation;
		}
		return false;
	}
	
	@Override
	public String toString(){
		return "(" + average + "," + deviation + ")";
	}

}
